<?php

namespace app\admin\model;

use think\Model;

class Flow extends Model
{
    // 表名
    protected $name = 'flow';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'add_time_text',
        'type_text',
        'dep_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getDepTextAttr($value, $data)
    {
        $dep = AuthDepartment::get($data['dep_id']);
        return $dep['name'];
    }



    public function getAddTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['add_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAddTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function getTypeList()
    {
        return [
            'test' => __('测试流程'),
            'order'=>'订单审核流程',
            'agent_bill'=>'代理商保证金',
            'finance_extract_task_outer'=>'外联任务单流程'
        ];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

}
